package com.hmall.order.config;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.OrderByPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 定期执行订单清除
 */
@Component
public class Task {

    @Autowired
    private OrderMapper orderMapper;

//    @Autowired
//    private OrderDetailMapper orderDetailMapper;
//    @Autowired
//    private ItemClient itemClient;

    //               秒  分  时 天 月 周
    @Scheduled(cron = "0 */1 * * * ?") // 每隔1分钟
    public void handleTast() {
        List<OrderByPojo> orderList = orderMapper.selectList(null);
        for (OrderByPojo orderByPojo : orderList) {
            if (orderByPojo.getStatus() == 1) {
                Date createTime = orderByPojo.getCreateTime();
                long time = createTime.getTime();

                long nowTime = System.currentTimeMillis();

                if ((nowTime - time) > 60000) {
                    LambdaUpdateWrapper<OrderByPojo> lambdaUpdate = Wrappers.lambdaUpdate(OrderByPojo.class);
                    lambdaUpdate.eq(OrderByPojo::getId, orderByPojo.getId()).set(OrderByPojo::getStatus, 5);

//                    LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
//                    queryWrapper.eq(OrderDetail::getOrderId, orderByPojo.getId());
//                    OrderDetail orderDetail = orderDetailMapper.selectOne(queryWrapper);
//                    itemClient.modifyStock(orderDetail.getItemId(), -orderDetail.getNum());

                    orderMapper.update(null, lambdaUpdate);
                }
            }
        }
    }
}
